package com.frxuexi.shop

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ProductBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.shop.databinding.ActivityShopBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/shop/ShopActivity")
@AndroidEntryPoint
class ShopActivity :BaseActivity<ActivityShopBinding,ShopViewModel>() {
//    lateinit var shopAdapter: ShopAdapter
    val shopPageAdapter by lazy { ShopPageAdapter() }
    override fun initData() {
        /*vm.productSuc.observe(this){
            Log.i("===",it.toString())
            shopAdapter.data.clear()
            shopAdapter.data.addAll(it.list)
            shopAdapter.notifyDataSetChanged()
        }*/
    }

    override fun initView() {
//        shopAdapter= ShopAdapter()
        binding.rvShop.adapter=shopPageAdapter
        binding.rvShop.layoutManager=GridLayoutManager(this,2)

        /*shopAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/shop/ShopDetailActivity").withSerializable("shop",shopAdapter.data.get(position)).navigation()
        }*/

        shopPageAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int, item: ProductBeanItem) {
                ARouter.getInstance().build("/shop/ShopDetailActivity").withSerializable("shop",item)
                    .navigation(this@ShopActivity,object :NavigationCallback{
                        override fun onFound(postcard: Postcard?) {

                        }

                        override fun onLost(postcard: Postcard?) {

                        }

                        override fun onArrival(postcard: Postcard?) {
                        }

                        override fun onInterrupt(postcard: Postcard?) {
                            ToastUtils.showLong("登录拦截")
                        }
                    })
            }
        })

        shopPageAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(item: ProductBeanItem) {
                ToastUtils.showLong(item.storeName)
            }
        })

//        vm.getProducts(1,10,1)
        lifecycleScope.launch {
            vm.getShopsPaging().collect{
                shopPageAdapter.submitData(it)
            }
        }

    }

}